﻿DiyWindow = {
  modal: function(options) {
    var setting = $.extend({
        id: '',
        title: '',
        width: 400,
        height: 300,
        url: '',
        fullScreen: false,
        btn: ['确定', '关闭'], //只是为了演示
        beginOpen: function() {},
        afterOpen: function() {},
        beginColse: function() { return true; },
        error: function(message) { alert(message); },
        onClose: function() {}
      },
      options);

    var currentWindow = window;

    //while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
    //  currentWindow = currentWindow.parent;
    //}


      //计算弹出窗口的实际高度和宽度
      var windowHeight = $(window).height();
        var windowWidth =$(window).width();

      if (windowHeight < setting.height) {
          setting.height = windowHeight - 40;
      }
    
      if (windowWidth < setting.width) {
          setting.width = windowWidth - 80;
      }

      currentWindow.layer.open({
          id: setting.id,
          type: 2,
          skin: 'layui-layer-molv',
          title: setting.title,
          area: [setting.width + 'px', setting.height + 'px'],
          shade: 0.3,
          maxmin: setting.showMaxBtn,
          anim: 1,
          content: setting.url.replace(/\"/g, "'"),
          zIndex: layer.zIndex, //重点1,
          success: function(layero) {
              layer.setTop(layero); //重点2
              setting.afterOpen();
          },
          cancel: function(index, layero) {
             // setting.onClose();
          },
          end: function(a,b){
              setting.onClose();
        }
    });
    },
  tab: function (name,url,onClose) {
    var currentWindow = window;

    while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
      currentWindow = currentWindow.parent;
    }

    currentWindow.addTabs(name, url,true, onClose);
  },
  closeTab: function () {
    var currentWindow = window;

    while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
      currentWindow = currentWindow.parent;
    }

    //var p = $(window).parent();
      var tabId = window.frameElement.dataset.id;

    currentWindow.closeTabById(tabId);
  },
  execParentFunc: function(funcName, para, afterFunc) {
    if (window.frameElement) {
      window.name = window.frameElement.name;
    }

    var currentWindow = window;

    while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
      currentWindow = currentWindow.parent;
    }

    var parentWindow = window.parent;

    if (parentWindow[funcName] != undefined) {
      parentWindow[funcName](para, afterFunc);
    } else {
      throw new Error("需要执行的方法【" + funcName + "】不存在");
    }

    //var zwindowElemt = currentWindow.$.fn.zwindow.getWindowById(window.name);
    //currentWindow.$.fn.zwindow.execWindowFunc(zwindowElemt.parentWindow.id,funcName, para, afterFunc);
  },
  closeModal: function() {
    if (window.frameElement) {
      window.name = window.frameElement.name;
    }

    var currentWindow = window.parent;

    //while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
    //  currentWindow = currentWindow.parent;
    //}
  
      var index = currentWindow.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

      currentWindow.layer.close(index); //再执行关闭  
  },
  closeTabOrModal: function() {
      if (window.frameElement) {
          window.name = window.frameElement.name;
      }

      var currentWindow = window;

      if (window.frameElement != null) {
          var tabId = window.frameElement.dataset.id;

          if (tabId != undefined && tabId != "") {

              while (currentWindow.name !== "DeskTop" &&
                  currentWindow.parent &&
                  currentWindow.parent !== currentWindow) {
                  currentWindow = currentWindow.parent;
              }
              currentWindow.closeTabById(tabId);
          } else {
              currentWindow = currentWindow.parent;

              var index = currentWindow.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

              currentWindow.layer.close(index); //再执行关闭  
          }
      }
  },
  hideModal: function() {
    if (window.frameElement) {
      window.name = window.frameElement.name;
    }

    var currentWindow = window;

    while (currentWindow.name !== "DeskTop" && currentWindow.parent && currentWindow.parent !== currentWindow) {
      currentWindow = currentWindow.parent;
    }

    var index = currentWindow.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    currentWindow.layer.close(index); //再执行关闭  
  }
};